package com.jingdianjichi.subject.application.controller;


import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    @Resource
    SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody @Validated SubjectLabelDTO subjectLabelDTO){
        try {
            log.info("添加标签: {}", subjectLabelDTO);
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("添加失败", e);
            return Result.fail();
        }
    }


    @PostMapping("/queryLabelList")
    public Result<List<SubjectLabelDTO>> queryLabelList(@RequestBody SubjectLabelDTO subjectLabelDTO){
        log.info("查询标签列表: {}", subjectLabelDTO.getSortNum());
        List<SubjectLabelBO> subjectLabelBOList
                = subjectLabelDomainService.queryLabelList(
                SubjectLabelDTOConverter.INSTANCE.convertDTOToBO(subjectLabelDTO));
        List<SubjectLabelDTO>subjectLabelDTOList = SubjectLabelDTOConverter.INSTANCE.convertBOListToDTOList(subjectLabelBOList);
        return Result.ok(subjectLabelDTOList);
    }


    @PostMapping("/deleteCategory/{id}")
    public Result<Boolean> deleteCategory(@PathVariable Long id){
        try {
            subjectLabelDomainService.deleteLabel(id);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("删除失败", e);
            return Result.fail();
        }
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody @Validated SubjectLabelDTO subjectLabelDTO) {
        if(subjectLabelDTO.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        try {
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("更新失败", e);
            return Result.fail();
        }
    }

    /**
     * 根据分类id查询分类下的标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/queryLabelsByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        log.info("根据分类id查询标签: {}", subjectLabelDTO.getCategoryId());
        SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
        List<SubjectLabelBO>subjectLabelBOS = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
        List<SubjectLabelDTO> labelDTOS = SubjectLabelDTOConverter.INSTANCE.convertBOListToDTOList(subjectLabelBOS);
        log.info("查询结果: {}", labelDTOS);
        return Result.ok(labelDTOS);
    }
}
